package com.jiangyao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jiangyao.manager.IntegralManager;
import com.jiangyao.common.exception.ServerException;
import com.jiangyao.common.utils.DateUtil;
import com.jiangyao.common.utils.FtpClient;
import com.jiangyao.dao.IUserExperienceDao;
import com.jiangyao.model.dto.ClassUserDto;
import com.jiangyao.model.dto.backend.UserExperienceDTO;
import com.jiangyao.model.dto.backend.UserExperienceDetailDTO;
import com.jiangyao.model.dto.backend.UserExperienceListPageDTO;
import com.jiangyao.model.entity.UserExperience;
import com.jiangyao.model.vo.AdminUserSessionVO;
import com.jiangyao.model.vo.ExperienceVo;
import com.jiangyao.model.vo.backend.UserExperienceListVO;
import com.jiangyao.service.IntegralLogService;
import com.jiangyao.service.UserExperienceService;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 成员个人经历 服务实现类
 * </p>
 *
 * @author xiangyan
 * @since 2020-08-17
 */
@Service
public class UserExperienceServiceImpl extends BaseServiceImpl<IUserExperienceDao, UserExperience> implements UserExperienceService {

	@Autowired
	private IntegralLogService integralLogService;

	@Autowired
	private IUserExperienceDao userExperienceDao;

	@Autowired
	private DozerBeanMapper dozerBeanMapper;

	@Autowired
	private IntegralManager integralManage;

	@Override
	public ExperienceVo getUserExperience(ClassUserDto dto) {
		ExperienceVo vo = new ExperienceVo();

		vo.setEducationIntegral(integralLogService.getIntegralSumByType(1));
		QueryWrapper<UserExperience> educationParams = new QueryWrapper();
		educationParams.eq("user_id", getLoginApiUserId()).eq("type", 1).eq("status", 2).orderByDesc("start_time");
		List<UserExperience> educationList = this.list(educationParams);
		if (!CollectionUtils.isEmpty(educationList)) {
			educationList.forEach(r -> r.setCredentials(FtpClient.fullFtpUrl(r.getCredentials())));
		}
		vo.setEducationList(educationList);

		vo.setWorkIntegral(integralLogService.getIntegralSumByType(2));
		QueryWrapper<UserExperience> workParams = new QueryWrapper();
		workParams.eq("user_id", getLoginApiUserId()).eq("type", 2).eq("status", 2).orderByDesc("start_time");
		List<UserExperience> workList = this.list(workParams);
		if (!CollectionUtils.isEmpty(workList)) {
			workList.forEach(r -> r.setCredentials(FtpClient.fullFtpUrl(r.getCredentials())));
		}
		vo.setWorkList(workList);

		vo.setSkillsIntegral(integralLogService.getIntegralSumByType(3));
		QueryWrapper<UserExperience> skillsParams = new QueryWrapper();
		skillsParams.eq("user_id", getLoginApiUserId()).eq("type", 3).eq("status", 2).orderByDesc("start_time");
		List<UserExperience> skillList = this.list(skillsParams);
		if (!CollectionUtils.isEmpty(skillList)) {
			skillList.forEach(r -> r.setCredentials(FtpClient.fullFtpUrl(r.getCredentials())));
		}
		vo.setSkillsList(skillList);

		vo.setRewardsIntegral(integralLogService.getIntegralSumByType(4));
		QueryWrapper<UserExperience> rewardsParams = new QueryWrapper();
		rewardsParams.eq("user_id", getLoginApiUserId()).eq("type", 4).eq("status", 2).orderByDesc("start_time");
		List<UserExperience> rewardsList = this.list(rewardsParams);
		if (!CollectionUtils.isEmpty(rewardsList)) {
			rewardsList.forEach(r -> r.setCredentials(FtpClient.fullFtpUrl(r.getCredentials())));
		}
		vo.setRewardsList(rewardsList);


		return vo;
	}

	@Override
	public Integer getUserExperienceWorkTear() {
		QueryWrapper<UserExperience> workParams = new QueryWrapper();
		workParams.eq("user_id", getLoginApiUserId()).eq("type", 2).eq("status", 2).orderByDesc("start_time");
		List<UserExperience> workList = this.list(workParams);
		long daySum = 0;
		for (UserExperience obj : workList) {
			if(obj.getStartTime()==null){
				continue;
			}
			daySum = daySum + DateUtil.daysBetweenTwoDate(obj.getStartTime(), obj.getEndTime() == null ? new Date() : obj.getEndTime());
		}
		int yearCount = (int) (daySum / 365);
		return yearCount;
	}


	@Override
	public Page<UserExperienceListVO> getListPage(UserExperienceListPageDTO listPageDTO) {
		// 登录用户信息
		AdminUserSessionVO adminUserSessionVO = getAdminLoginSession();
		// 当前登录用户管理的组织ID集合(业务数据权限)
		listPageDTO.setOrgIdList(adminUserSessionVO.getOrgIdList());
		Page<UserExperienceListVO> page = new Page<>(listPageDTO.getPageNo(), listPageDTO.getPageSize());
		userExperienceDao.getListPage(page, listPageDTO);
		if (!CollectionUtils.isEmpty(page.getRecords())) {
			page.getRecords().forEach(vo -> vo.setCredentialsFull(FtpClient.fullFtpUrl(vo.getCredentials())));
		}
		return page;
	}

	@Override
	public void add(UserExperienceDTO addDTO) {

		UserExperience userExperience = dozerBeanMapper.map(addDTO, UserExperience.class);
		userExperience.setId(null);
		userExperience.setStatus(2);
		userExperience.setCreateTime(new Date());
		this.save(userExperience);
		// 保存积分流水日志-个人档案
		integralManage.saveIntegralLogForUserExperience(Lists.newArrayList(userExperience.getId()));
	}

	@Override
	public void edit(UserExperienceDTO editDTO) {
		UserExperience experience = this.getById(editDTO.getId());
		if (experience == null) {
			throw new ServerException(100, "个人档案不存在");
		}

		UpdateWrapper<UserExperience> uw = new UpdateWrapper<>();
		uw.eq("id", editDTO.getId());
		uw.set("type", editDTO.getType());
		uw.set("start_time", editDTO.getStartTime());
		uw.set("end_time", editDTO.getEndTime());
		uw.set("content", editDTO.getContent());
		uw.set("credentials", editDTO.getCredentials());
		uw.set("experience", editDTO.getExperience());
		this.update(uw);
	}

	@Override
	public void editStatus(UserExperienceDTO auditDTO) {
		UpdateWrapper<UserExperience> uw = new UpdateWrapper<>();
		uw.in("id", auditDTO.getIdList());
		uw.set("status", auditDTO.getStatus());
		uw.set("audit_admin_id", getLoginAdminUserId());
		uw.set("audit_time", new Date());
		this.update(uw);
		if (auditDTO.getStatus() == 2) {
			// 保存积分流水日志-个人档案
			integralManage.saveIntegralLogForUserExperience(auditDTO.getIdList());
		}
	}

	@Override
	public UserExperienceListVO getDetail(UserExperienceDetailDTO detailDTO) {
		UserExperienceListVO vo = userExperienceDao.getDetail(detailDTO);
		if (vo == null) {
			return null;
		}
		vo.setCredentialsFull(FtpClient.fullFtpUrl(vo.getCredentials()));
		return vo;
	}
}
